<template>
	<view class="card_list">
		<view
			style="background-color: #fff; padding:10px;display: flex;align-items: center;justify-content: space-between;">
			<input type="text" placeholder="请输入搜索内容" v-model="searchStation" class="search-station" />
			<a @click="onSearchInput">确定</a>
		</view>
		<view class="card" v-for="(item, index) in showAlarmList" :key="index"
			@click="goDetail(item.deviceSn, item.alarmTitle, item.id)">
			<uni-card margin=" 0" padding="0" spacing="0">
				<template v-slot:title>
					<view class="title">
						<view class="left">
							<view class="left_warn">警告</view>
							<view class="left_title">{{ item.alarmTitle }}</view>
						</view>
						<view class="right">{{item.status == 1 ? "发生中" : "已恢复"}}</view>
					</view>
				</template>
				<view class="center">
					<view class="c_left">
						<view class="c_title">电站</view>
						<view class="word">{{ item.stationName }}</view>
					</view>
					<view class="c_right">
						<view class="r_title">设备</view>
						<view class="word">{{ item.deviceSn }}</view>
					</view>
				</view>
				<uni-list>
					<uni-list-item>
						<template v-slot:header>
							<view class="warn_time">
								<text class="iconfont icon-rili"></text>
								<text style="padding: 0 10rpx">报警开始时间</text>
								<text>{{ item.createTime }}</text>
							</view>
						</template>
					</uni-list-item>
				</uni-list>
			</uni-card>
		</view>
		<o-empty style="background-color: #f3f4f6" v-if="!showAlarmList.length"></o-empty>
		<!-- 点击加载更多 -->
		<uni-load-more v-if="showAlarmList.length" :status="loadingStatus"></uni-load-more>
	</view>
</template>

<script>
	import {
		getAlarmQueryList
	} from "@/common/api/api.js"
	export default {
		props: {
			reach: Boolean,
			current: Number,
		},
		data() {
			return {
				alarmList: [],
				showAlarmList: [],
				page: 1,
				count: 0,
				loadingStatus: "more",
				searchStation: '',
				tabCurrent: ''
			}
		},
		watch: {
			reach() {
				this.onReach()
			},
		},
		methods: {
			onSearchInput() {
				this.getData(this.tabCurrent)
			},
			//上拉刷新处理
			async onReach() {
				if (!this.count) {
					return
				}
				if (Array.isArray(this.alarmList) && this.alarmList.length >= this.count) {
					return (this.loadingStatus = "no-more")
				}
				this.loadingStatus = "loading"
				await new Promise((resolve) => setTimeout(resolve, 300))
				const token = uni.getStorageSync("Token")
				let params = {
					token: token,
					alarm: {},
					page: this.page + 1,
					limit: 10,
				}
				if (this.device_id) {
					params.id = this.device_id
				}
				let res = await getAlarmQueryList(params)
				if (Array.isArray(res.data) && res.data.length) {
					this.loadingStatus = "more"
					this.page++
					this.alarmList = [...this.alarmList, ...res.data]
					this.showAlarmList = JSON.parse(JSON.stringify(this.alarmList))
				} else {
					this.loadingStatus = "no-more"
				}
			},
			//跳转详情页
			goDetail(deviceSn, alarmTitle, id) {
				console.log('deviceSn, alarmTitle, id', deviceSn, alarmTitle, id);
				uni.navigateTo({
					url: `/pages/alarm/index?deviceSn=${deviceSn}&alarmTitle=${alarmTitle}&id=${id}`,
				})
			},
			//初始化数据
			async getData(val) {
				this.tabCurrent = val
				let token = uni.getStorageSync("Token")
				let params = {
					token: token,
					alarm: {},
					page: this.page,
					limit: 10,
					status: val,
					stationName: this.searchStation
				}
				let res = await getAlarmQueryList(params)
				this.alarmList = res.data
				this.showAlarmList = res.data
				this.count = res.count
				if (Array.isArray(this.alarmList) && this.alarmList.length >= this.count) {
					this.loadingStatus = "no-more"
				}
			},
		},
		mounted() {
			this.getData('1')
		},
	}
</script>

<style scoped lang="scss">
	.search-station {
		border: 1px solid #ccc;
		border-radius: 10px;
		margin: 0 10px;
		background: #f9f9f9;
		width: 80%;
	}

	.card_list {
		.card {
			margin-top: 20rpx;
		}

		.title {
			padding: 10rpx 20rpx;
			display: flex;
			justify-content: space-evenly;
			height: 80rpx;
			align-items: center;

			.left {
				width: calc(100% - 100rpx);
				align-items: center;
				display: flex;

				.left_warn {
					font-size: 12rpx;
					background-color: #e49042;
					padding: 4rpx 8rpx;
					color: #fff;
					margin-right: 10rpx;
				}

				.left_title {
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				text-align: right;
				width: auto;
				font-size: 24rpx;
				color: darkred;
				font-weight: bold;
			}
		}

		.center {
			padding: 0 20rpx;
			display: flex;

			.c_left {
				width: 50%;

				.c_title {
					padding: 10rpx 0;
					font-size: 24rpx;
				}

				.word {
					font-size: 28rpx;
				}
			}

			.c_right {
				width: 50%;

				.r_title {
					padding: 10rpx 0;
					font-size: 24rpx;
				}

				.word {
					font-size: 28rpx;
				}
			}
		}

		.warn_time {
			font-size: 24rpx;

			.iconfont {
				font-size: 24rpx !important;
			}
		}
	}
</style>